var db = require('../utils/db');

class PositionModel {

  constructor() {
    this.model = db.model('positions', {
      positionName: String,
      city: String,
      createTime: String,
      salary: String,
      companyLogo: String,
      companyName: String
    })
  }


  save(data) {
    let posModel = new this.model(data);
    return posModel.save()
  }

  /**
   * 职位列表查询
   * @param {*} searchParams 查询条件
   * @param {*} pagesize 每页条数
   * @param {*} pageno 第几页
   * @param {*} sortname 排序字段
   * @param {*} sort asc | desc
   */
  query(searchParams, pagesize, pageno, sortname, sort) {
    console.log(searchParams, pagesize, pageno, sortname, sort)
    return this.model.find(searchParams)
      .limit(pagesize * 1)
      .skip((pageno) * pagesize)
      .sort({ [sortname]: sort === 'asc' ? 1 : -1 })


  }

  count(params) {
    // let reg = new RegExp(keyword, 'i');
    // return this.model.find({ $or: [{ companyName: reg }, { positionName: reg }] }).count()
    return this.model.find(params).count()
  }

  findone(_id) {
    return this.model.findOne({ _id })
  }

  update(_id, data) {
    console.log(_id, data)
    return this.model.updateOne({ _id }, { $set: data })
  }

  remove(_ids) {
    console.log(_ids)
    return this.model.deleteMany({ _id: { $in: _ids } })
  }

}

module.exports = new PositionModel();